Okay.
In this lecture, we're going to quickly set up a collision group for players in our game to avoid player
collisions.
Now we should be well versed with collision groups at this point.
So this will be a piece of cake to implement in our project.
We can go ahead and head to our model tab and open up collision groups and create a new collision group
in here.
I'm just going to call it players, and we'll make sure to have players not collide with other players.
Now that we have our new collision group, we can head to Server Script service and add a new module,
script or service.
And I'm just going to call this our collision Group service.
We'll use that same template we've been using before.
I'll rename this to collision Group service.
And inside of here we're going to need the player service.
And we should be good to go.
So what we're going to need is a function to listen for when a character is added to the game.
We'll pass a character to this function.
We're also going to want to listen for when a descendant is added to our player's character.
So we'll call it on, descendant added.
And then we'll also want to listen to when a descendant is removed from our player's character.
That way we remove the collision group off of that particular instance.
So on descendant removed and we'll pass a descendant to this function as well as to this function.
And then we'll also create a function to listen for.
When a player is added to the game, we'll pass a player to this function.
And then with this player we'll listen to their character added event, and we'll connect the on character
added function to that event.
And then for the initialize function, all we're going to need to do is loop through every single player
that is inside of our game.
So in players get players.
And we'll call our on player added and pass that player to the function.
And then we're also going to connect to the players dot player, added event and connect on player added.
So when a character is added to a specific player, what we want to do is loop through every single
descendant inside of that character.
So in character, get descendants.
What we want to do is we'll use our en descendant added function.
So we'll call en descendant added and pass that particular descendant.
And then for this character, we want to go ahead and connect to the descendant added event as well
as the descendant removing event.
So we'll connect en descendant added and then we'll connect to the descendant removing event.
On the Senate removed.
And then all we're going to do in this function is we're going to make sure that this descendant is
a base part.
So is a base part.
If it is not a bass part, then we're just going to return.
Otherwise we can set this descendant collision group property equal to our players collision group.
And then we're going to copy this here and do the same thing for descendant removing.
But this time we want to set the collision group back to the default collision group.
And just like that we're done with setting up collision groups for our game.
So let's go ahead and start up a two player server and see if it works.
All right here.
I've got my two players and let's see if I can collide with each other.
So if I go and walk.
Actually looks like we're still colliding for some reason.
That's a problem.
So let's go ahead and see what we did wrong in our code.
And in fact, we did get an error.
And our error is down here because I accidentally made a little typo.
I put a capital L there instead of a lowercase l, so that's why it didn't work.
So make sure your get players call does not have a capital L in it.
And now we should be able to test with the two player server.
Okay let's go ahead and try that one more time.
If I go and touch my player, as you can see we no longer have collisions with our players.
Very easy to implement.
Now our project should be complete, so I'll go ahead and see you in the next lecture.